package com.mayblackcat.dy.service.impl;

import com.mayblackcat.dy.dao.CommentDao;
import com.mayblackcat.dy.entity.Comment;
import com.mayblackcat.dy.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author ASUS
 */
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentDao commentDao;

    /**存放迭代找出的所有子代的集合*/
    private List<Comment> tempReplys = new ArrayList<>();

    /**
     * @Description: 查询留言
     * @Param:
     * @Return: 留言消息
     */

    public List<Comment> listComment() {
        //查询出父节点
        List<Comment> Comment = commentDao.findByParentIdNull(Long.parseLong("-1"));
        for (Comment comment : Comment) {
            Long id = comment.getId();
            String parentNickname1 = comment.getNickName();
            List<Comment> childComment = commentDao.findByParentIdNotNull(id);
            //查询出子留言
            combineChildren(childComment, parentNickname1);
            comment.setReplyComment(tempReplys);
            tempReplys = new ArrayList<>();
        }
        return Comment;
    }

    /**
     * @Description: 查询出子留言
     * @Param: childComment：所有子留言
     * @Param: parentNickname1：父留言的姓名
     * @Return:
     */
    private void combineChildren(List<Comment> childComment1, String parentNickname1) {
        //判断是否有一级子回复
        if (childComment1.size() > 0) {
            //循环找出子留言的id
            for (Comment childComment : childComment1) {
                String parentNickname = childComment.getNickName();
                childComment.setParentNickname(parentNickname1);
                tempReplys.add(childComment);
                Long childId = childComment.getId();
                //查询二级以及所有子集回复
                recursively(childId, parentNickname);
            }
        }
    }

    /**
     * @Description: 循环迭代找出子集回复
     * @Param: childId：子留言的id
     * @Param: parentNickname1：子留言的姓名
     * @Return:
     */
    private void recursively(Long childId, String parentNickname1) {
        //根据子一级留言的id找到子二级留言
        List<Comment> replayComment1 = commentDao.findByReplayId(childId);

        if (replayComment1.size() > 0) {
            for (Comment replayComment : replayComment1) {
                String parentNickname = replayComment.getNickName();
                replayComment.setParentNickname(parentNickname1);
                Long replayId = replayComment.getId();
                tempReplys.add(replayComment);
                //循环迭代找出子集回复
                recursively(replayId, parentNickname);
            }
        }
    }


    //存储留言信息
    @Transactional
    public int saveComment(Comment comment) {
        comment.setCreateTime(new Date());
        return commentDao.saveComment(comment);
    }

    //    删除留言
    @Transactional
    public void deleteComment(Long id) {
        commentDao.deleteComment(id);
    }

}
